package pdfsetter.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.schema.AutomaticDocumentSchemaPattern;
import pdfsetter.core.schema.CompoundDocumentSchemaPattern;
import pdfsetter.core.schema.DocumentSchemaPattern;
import pdfsetter.core.schema.SimpleDocumentSchemaPattern;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.core.schema.advanced.AttachedFilePageSelector;
import pdfsetter.core.schema.advanced.AutomaticConsecutiveProcessPattern;
import pdfsetter.core.schema.advanced.AutomaticRepetitiveProcessPattern;
import pdfsetter.core.schema.advanced.ConsecutiveFileListProcessPattern;
import pdfsetter.core.schema.advanced.FilePageSelector;
import pdfsetter.core.schema.advanced.FillingSchemaPattern;
import pdfsetter.core.schema.advanced.RepetitiveFileListProcessPattern;
import pdfsetter.core.schema.onefile.OneFileDocumentSchemaPattern;
import pdfsetter.core.schema.onefile.SimpleOneFileDocumentSchemaPattern;
import pdfsetter.core.schema.selectors.CompoundFilePageSelector;
import pdfsetter.core.schema.selectors.JoinedFilePageSelector;
import pdfsetter.core.schema.selectors.LeapingPageSelector;
import pdfsetter.core.schema.selectors.MultiplyingPageSelector;
import pdfsetter.core.schema.selectors.NormalPageSelector;
import pdfsetter.core.schema.selectors.RangePageSelector;
import pdfsetter.core.schema.selectors.ReversingPageSelector;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.XStreamException;

public class XMLObjectSerializer {
    private final XStream xstream;

    private void initializeAliases() {
        // SinglePageSchema with components
        xstream.alias("PageRectangle", PageRectangle.class);
        xstream.alias("PageInfo", PageInfo.class);
        xstream.alias("SinglePageSchema", SinglePageSchema.class);
        // FilePageSelectors
        xstream.alias("FilePageSelector", FilePageSelector.class);
        xstream.alias("MultiplyingPageSelector", MultiplyingPageSelector.class);
        xstream.alias("LeapingPageSelector", LeapingPageSelector.class);
        xstream.alias("RangePageSelector", RangePageSelector.class);
        xstream.alias("NormalPageSelector", NormalPageSelector.class);
        xstream.alias("ReversingPageSelector", ReversingPageSelector.class);
        xstream.alias("CompoundFilePageSelector", CompoundFilePageSelector.class);
        xstream.alias("JoinedFilePageSelector", JoinedFilePageSelector.class);
        // FillingSchemaPatterns and components
        xstream.alias("FillingSchemaPattern", FillingSchemaPattern.class);
        xstream.alias("AutomaticConsecutiveProcessPattern", AutomaticConsecutiveProcessPattern.class);
        xstream.alias("AutomaticRepetitiveProcessPattern", AutomaticRepetitiveProcessPattern.class);
        xstream.alias("ConsecutiveFileListProcessPattern", ConsecutiveFileListProcessPattern.class);
        xstream.alias("RepetitiveFileListProcessPattern", RepetitiveFileListProcessPattern.class);
        xstream.alias("AttachedFilePageSelector", AttachedFilePageSelector.class);
        // DocumentSchemaPatterns
        xstream.alias("DocumentSchemaPattern", DocumentSchemaPattern.class);
        xstream.alias("SimpleDocumentSchemaPattern", SimpleDocumentSchemaPattern.class);
        xstream.alias("CompoundDocumentSchemaPattern", CompoundDocumentSchemaPattern.class);
        xstream.alias("AutomaticDocumentSchemaPattern", AutomaticDocumentSchemaPattern.class);
        // OneFileDocumentSchemaPatterns
        xstream.alias("OneFileDocumentSchemaPattern", OneFileDocumentSchemaPattern.class);
        xstream.alias("SimpleOneFileDocumentSchemaPattern", SimpleOneFileDocumentSchemaPattern.class);
    }

    public XMLObjectSerializer() {
        xstream = new XStream();
        initializeAliases();
    }

    public void writeObject(Object object, String path) throws FileNotFoundException, XStreamException {
        xstream.toXML(object, new FileOutputStream(path));
    }

    public Object readObject(String path) throws XStreamException {
        return xstream.fromXML(new File(path));
    }
}
